package xyz.scootaloo.mono.security.aop

import org.springframework.context.annotation.Configuration
import org.springframework.http.converter.HttpMessageConverter
import org.springframework.http.converter.StringHttpMessageConverter
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

/**
 * 由于这个模块重写了 WebConfigurer
 * 覆盖了 spring 的默认配置, 导致 controller 的转换器缺失,
 * 这里将缺失的转换器补充回来.
 *
 * 转换器, HttpMessageConverter
 * 用于把controller中标记为 [GetMapping] 或 [PostMapping] 这样的方法的返回值转换成http报文的响应体
 *
 * 例如方法返回 [String] 需要 [StringHttpMessageConverter],
 * 方法返回对象需要 [MappingJackson2HttpMessageConverter]
 *
 * @author flutterdash@qq.com
 * @since 2021/8/2 16:18
 */
@Configuration
class ConverterConfiguration : WebMvcConfigurer {

    override fun configureMessageConverters(converters: MutableList<HttpMessageConverter<*>>) {
        converters.add(responseBodyConverter())
        converters.add(stringHttpConverter())
    }

    fun responseBodyConverter(): HttpMessageConverter<Any> =
        MappingJackson2HttpMessageConverter()

    fun stringHttpConverter(): StringHttpMessageConverter =
        StringHttpMessageConverter()
}
